package com.hele.sts.action.admin;

import com.hele.sts.dao.QUserDao;
import com.hele.sts.dao.UserDao;
import com.hele.sts.pub.R;
import com.hele.sts.pub.encrypt.BCryptPasswordEncoder;
import com.opensymphony.xwork2.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName AdminUserMgrAction
 * @Description TODO
 * @Author xuning
 * @Date 2019-06-20 06:53
 **/
public class AdminUserMgrAction implements Action {

    private static final Logger logger = LoggerFactory.getLogger(AdminUserMgrAction.class);

    private static final String DEFAULT_PASSWORD = "Sts123";

    private String action;
    private String query;

    private List suggestions;

    private String selectedUserName;

    private R ret;

    @Override
    public String execute() throws Exception {
        if (action == null) {
            action = "init";
            return "init";
        }
        if ("searchUsername".equals(action)) {
            suggestions = UserDao.search(query);
            return "ajax";
        }
        if ("resetPassword".equals(action)) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String newPassword = passwordEncoder.encode(DEFAULT_PASSWORD);
            int result = UserDao.updatePassword(selectedUserName, newPassword);
            if (result > -1) {
                ret = R.ok("重置密码成功");
                logger.info("{},重置密码成功", selectedUserName);
            } else {
                ret = R.error("重置密码成功");
                logger.info("{},重置密码失败", selectedUserName);
            }
            return "ajax";
        }
        return null;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public List getSuggestions() {
        return suggestions;
    }

    public void setSuggestions(List suggestions) {
        this.suggestions = suggestions;
    }

    public String getSelectedUserName() {
        return selectedUserName;
    }

    public void setSelectedUserName(String selectedUserName) {
        this.selectedUserName = selectedUserName;
    }

    public R getRet() {
        return ret;
    }

    public void setRet(R ret) {
        this.ret = ret;
    }
}
